package com.keyinong.caizhengting.tools;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;

public class MyGPSPosition {
    static double latitude;
    static double longitude;
    static LocationManager locationManager;
    static Location location = null;
    private static LocationListener listener = new LocationListener() {
        // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
        @Override
        public void onStatusChanged(String provider, int status,
                                    Bundle extras) {
            // TODO Auto-generated method stub

        }

        // Provider被enable时触发此函数，比如GPS被打开
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        // Provider被disable时触发此函数，比如GPS被关闭
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            gettode(null);
        }

        // 当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub

        }
    };

    public static double[] getposition(final Context context) {
        // PackageManager pm = context.getPackageManager();
        // boolean permission = (PackageManager.PERMISSION_GRANTED == pm
        // .checkPermission("android.permission.ACCESS_FINE_LOCATION",
        // "GPS"));
        // if(permission==false){
        // }

//		Intent settingsIntent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//		context.startActivity(settingsIntent);

        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            // 通过GPS获取定位的位置数据
            location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        }

        // 获得位置服务的管理对象
        gettode(location);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000, 8, listener);

        /*
         * else { Intent GPSIntent = new Intent();
         * GPSIntent.setClassName("com.android.settings",
         * "com.android.settings.widget.SettingsAppWidgetProvider");
         * GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
         * GPSIntent.setData(Uri.parse("custom:3")); try {
         * PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send(); } catch
         * (CanceledException e) { e.printStackTrace(); }
         *
         * // 通过GPS获取定位的位置数据 Location location = locationManager
         * .getLastKnownLocation(locationManager.GPS_PROVIDER);
         *
         * latitude = location.getLatitude(); longitude =
         * location.getLongitude();
         *
         * }
         */
        return (new double[]{latitude, longitude});
    }

    private static void gettode(Location location) {
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }

    public static final boolean isOPen(final Context context) {
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }

        return false;
    }

    public static final void openGPS(Context context) {
        Intent GPSIntent = new Intent();
        GPSIntent.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
        GPSIntent.setData(Uri.parse("custom:3"));
        try {
            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
        } catch (CanceledException e) {
            e.printStackTrace();
        }
    }


    public static void destroy(Context context) {
        if (locationManager != null) {
            locationManager.removeUpdates(listener);
        }
        locationManager = null;
        if (listener != null) {
//		listener=null;
        }
//		ContentResolver resolver = context.getContentResolver();
//		boolean enabled = getGpsState(context);
//		Settings.Secure.setLocationProviderEnabled(resolver,
//				LocationManager.GPS_PROVIDER, !enabled);
//		Settings.Secure.setLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER, false);
    }

}
